class ActiveRecord::Base
  class_inheritable_accessor :save_when_not_dirty
  
  attr_reader :original_attributes
  
  def dirty?
    attributes != original_attributes
  end
  
  private
  
  alias_method :create_or_update_before_dirty_checking, :create_or_update
  
  def create_or_update
    if dirty? || self.class.save_when_not_dirty
      result = create_or_update_before_dirty_checking
      if result
        @original_attributes = attributes
      end
      result
    else
      true
    end
  end
  
  class << self
    alias_method :instantiate_before_dirty_checking, :instantiate
    
    def instantiate( record )
      object = instantiate_before_dirty_checking( record )
      object.instance_variable_set( '@original_attributes', object.attributes )
      object
    end
  end
end